package com.mysqlpass.update.controller;

import com.mysqlpass.update.model.ApiResponse;
import com.mysqlpass.update.model.UpdateInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * ApiResponse使用示例控制器
 * 演示如何使用ApiResponse封装不同类型的业务实体
 */
@RestController
@RequestMapping("/api-example")
public class ApiResponseExampleController {

    /**
     * 示例1: 封装UpdateInfo类型的业务实体
     */
    @GetMapping("/update-info")
    public ResponseEntity<ApiResponse<UpdateInfo>> getUpdateInfo() {
        // 创建业务实体
        UpdateInfo updateInfo = new UpdateInfo();
        updateInfo.setCurrentVersion("1.0.0");
        updateInfo.setLatestVersion("1.1.0");
        updateInfo.setNeedUpdate(true);
        updateInfo.setUpdateDescription("新版本发布，包含性能优化和bug修复");
        updateInfo.setDownloadUrl("/update/download");
        updateInfo.setReleaseDate(new Date());
        
        // 使用ApiResponse封装
        ApiResponse<UpdateInfo> response = ApiResponse.success("获取更新信息成功", updateInfo);
        
        return ResponseEntity.ok(response);
    }

    /**
     * 示例2: 封装基本数据类型
     */
    @GetMapping("/basic-data")
    public ResponseEntity<ApiResponse<String>> getBasicData() {
        // 使用ApiResponse封装字符串
        return ResponseEntity.ok(ApiResponse.success("这是一个基本字符串响应数据"));
    }

    /**
     * 示例3: 封装集合类型
     */
    @GetMapping("/list-data")
    public ResponseEntity<ApiResponse<List<String>>> getListData() {
        List<String> dataList = Arrays.asList(
                "示例数据项1",
                "示例数据项2",
                "示例数据项3"
        );
        
        return ResponseEntity.ok(ApiResponse.success("获取列表数据成功", dataList));
    }

    /**
     * 示例4: 封装复杂对象类型
     */
    @GetMapping("/complex-data")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getComplexData() {
        Map<String, Object> complexData = new HashMap<>();
        complexData.put("id", 123);
        complexData.put("name", "复杂数据对象");
        complexData.put("active", true);
        complexData.put("tags", Arrays.asList("标签1", "标签2"));
        complexData.put("createdTime", new Date());
        
        return ResponseEntity.ok(ApiResponse.success("获取复杂数据成功", complexData));
    }

    /**
     * 示例5: 错误响应
     */
    @GetMapping("/error-example")
    public ResponseEntity<ApiResponse<Void>> getErrorExample(
            @RequestParam(required = false) Boolean throwError) {
        
        if (Boolean.TRUE.equals(throwError)) {
            // 返回错误响应
            return ResponseEntity.badRequest()
                    .body(ApiResponse.fail(400, "请求参数错误"));
        }
        
        // 返回成功响应（没有数据体）
        return ResponseEntity.ok(ApiResponse.success("操作成功", null));
    }

    /**
     * 示例6: 使用路径变量和自定义状态码
     */
    @GetMapping("/custom-status/{id}")
    public ResponseEntity<ApiResponse<String>> getCustomStatus(@PathVariable Long id) {
        if (id <= 0) {
            return ResponseEntity.status(400)
                    .body(ApiResponse.fail(400, "ID必须为正数"));
        }
        
        return ResponseEntity.ok(
                ApiResponse.success("自定义状态码示例", "ID: " + id)
        );
    }

    /**
     * 示例7: 日期格式的响应
     */
    @GetMapping("/date-format")
    public ResponseEntity<ApiResponse<Map<String, Date>>> getDateFormat() {
        Map<String, Date> dateMap = new HashMap<>();
        dateMap.put("currentDate", new Date());
        
        // 添加一个未来日期示例
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, 7);
        dateMap.put("nextWeekDate", calendar.getTime());
        
        return ResponseEntity.ok(ApiResponse.success("日期格式响应示例", dateMap));
    }
}